import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

const actions = {};
const mutations = {
  handleUserName: (username) => {
    console.log(username)
    // 把登录的用户的名保存到localStorage中，防止页面刷新，导致vuex重新启动，用户名就成为初始值（初始值为空）的情况
    localStorage.setItem('userid', username);
    state.username = username;    
  },
  increment (state) {
    state.count++
  }
};
const state = {
  username: "" || localStorage.getItem("userid"),
  count: 0
};
// getters 只会依赖 state 中的成员去更新
const getters = {
  userName: state => {
    return state.username
  }
};

export default new Vuex.Store({
  actions,
  mutations,
  state,
  getters
});
